#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct pair{
  int value;
  int num;
};
#define MAX 1005
static struct pair head[MAX];
static struct pair out[MAX];
static int pos = 0,total = 0;
int outindex = 0;
int linelen =0;
int tvalue,tnum;
void output(void);
void init(struct pair pairs[])
{
  int i=0;
  for(i=0;i<MAX;i++)
  {
    pairs[i].num=0;
    pairs[i].value=0;
  }
}
int main(int argc,char* argv[])
{
  int i,j,k;
  while(EOF!=scanf("%d",&linelen))
  {
    printf("%d\n",linelen);
    if(linelen==0)break;
    i=0;
    while(EOF!=scanf("%d%d",&tvalue,&tnum))
      {
        if(tnum==0&&tvalue==0)
          break;
        head[i].value = tvalue;
        head[i].num = tnum;
        i++;
        total+=tnum;
      }
    head[i].value = 0;
    head[i].num=0;
    output();
    print(out);
    outindex=0;
    pos=0;
    total=0;
    init(out);
  }
  return 0;
}
#define MAIN 0
#define INDEX 0
#define LT 1
#define MT 2
#define RT 3
#define LM 4
#define RM 5
#define LB 6
#define MB 7
#define RB 8

int __getnumgo(int* index, int* num)
{
  int result;
  if((*num)==0)
  {
    return -1;
  }
  if(head[*index].num==0)
    return EOF;
  result = head[*index].value;
  (*num)++;
  if(*num>head[*index].num)
  {
    (*num) = 1;
    (*index)++;
  }
  return result;
  
}
int __addnum(int value)
{
  if(out[outindex].value==value)
  {
    out[outindex].num++;
    return 0;
  }
  if(out[outindex].num==0)
  {
    out[outindex].num++;
    out[outindex].value = value;
    return 0;
  }
  outindex++;
  __addnum(value);
  return 0;
}
int __calresult(int* outint)
{
  int i,result=0;
  int t;
  for(i=1;i<9;i++)
  {
    if(*(outint+i)==-1)
    {
      continue;
    }
    int t = abs(*(outint+i)-*outint);
    if(t>result)
    {
      result = t;
    }
  }
  return result;
}

int print(struct pair toprint[])
{
  int index=0;
  for(index=0;toprint[index].num!=0;index++)
  {
    printf("%d %d\n",toprint[index].value,toprint[index].num);
  }
  printf("0 0\n");
}
void __getpos(int pos,int* gindex,int* gp )
{
  int i,j;
  (*gindex)=0;
  (*gp)=1;
  for(i=0;i<pos;i++)
  {
    if(head[*gindex].num-*gp>0)
      (*gp)+=1;
    else
    {
      (*gindex)+=1;
      (*gp) = 1;
    }
  }
  return;
}

int __cmpint(void* a,void *b)
{
  return *(int*)a-*(int*)b;
}
void output()
{
  int indexint[9]={0};
  int posint[9]={0};
  int outint[9]={0};
  int aint[9]={0};
  int poscount=0;
  int i=0,j=0,k=0;
  __getpos(linelen,&indexint[MB],&posint[MB]);
  __getpos(linelen+1,&indexint[RB],&posint[RB]);
  __getpos(1,&indexint[RM],&posint[RM]);
  posint[0]=1;
  while(head[indexint[MAIN]].num!=0)
  {
    while(1)
    {
      if(0==poscount%linelen&&out[outindex].value==0&&out[outindex].num>(linelen))
      {
        int t = head[indexint[INDEX]].num-(out[outindex].num+linelen*2);
        int i;
        if(t>0)
        {
          for(i=0;i<9;i++)
          {
            posint[i]+=t;
          }
          out[outindex].num+=t;
	  poscount+=t;
        }
        
      }
      
      if(out[outindex].num>3&&poscount%linelen>=2&&poscount%linelen<linelen-2
	 &&posint[RT]>=3&&posint[RM]>=3&&posint[RB]>=3)
	{
	  int fourint[4]={
	    posint[RT]!=0?head[indexint[RT]].num-posint[RT]:0,
	    posint[RM]!=0?head[indexint[RM]].num-posint[RM]:0,
	    posint[RB]!=0?head[indexint[RB]].num-posint[RB]:0,
	    linelen-poscount%linelen-2};
	  qsort(fourint,4,sizeof(int),__cmpint);
        if(fourint[0]>0)
        {
          for(i=0;i<9;i++)
          {
	    if(posint[i]>0)
	      posint[i]+=fourint[0];
          }
	  poscount+=fourint[0];
          out[outindex].num+=fourint[0];
        }

	  
	}
      
      outint[MAIN]= head[indexint[INDEX]].value;
      if(poscount==1)
      {
        __getpos(0,&indexint[LM],&posint[LM]);
        __getpos(linelen,&indexint[LB],&posint[LB]);
      }
      if(poscount==linelen)
      {
        __getpos(0,&indexint[MT],&posint[MT]);
        __getpos(1,&indexint[RT],&posint[RT]);
      }
      if(poscount==linelen+1)
      {
        __getpos(0,&indexint[LT],&posint[LT]);
      }
      for(i=1;i<9;i++)
      {
        outint[i]=__getnumgo(&indexint[i],&posint[i]);
      }
      if(poscount<linelen)
      {
        outint[LT]=outint[MAIN];
        outint[MT]=outint[MAIN];
        outint[RT]=outint[MAIN];
      }
      if(0==(poscount%linelen))
      {
        outint[LT]=outint[MAIN];
        outint[LM]=outint[MAIN];
        outint[LB]=outint[MAIN];
      }
      if(0==((poscount+1)%linelen))
      {
        outint[RT]=outint[MAIN];
        outint[RM]=outint[MAIN];
        outint[RB]=outint[MAIN];
      }
      if((total-poscount)<=linelen)
      {
        outint[LB]=outint[MAIN];
        outint[MB]=outint[MAIN];

        outint[RB]=outint[MAIN];
      }
      posint[INDEX]++;
      __addnum(__calresult(outint));
      poscount++;
      if(posint[INDEX]>head[indexint[INDEX]].num)break;
    }
    posint[INDEX]=1;
    indexint[MAIN]++;
  }
  
}
